package pushmsg

import (
	"gitee.com/sailinst/utils/random"
	"net/http"
	"net/url"
	"strings"
	"time"
)

var (
	BIND_Tag   = "BindTag"
	UNBIND_Tag = "UnbindTag"
	LIST_Tags  = "ListTags"
	QUERY_Tags = "QueryTags"
)

type TagInfo struct {
	RoomKey        string
	UserName       string
	EnterpriseName string
}

func (tg *TagInfo) GetTagapi(action string) (bool, *AliRespInfo) {
	resp := new(AliRespInfo)
	var ok bool
	switch action {
	case "BindTag":
		if ok, resp = AddAliMessageTag("ACCOUNT", tg.RoomKey, tg.UserName); ok {
			return ok, resp
		}
		return false, resp
	case "UnbindTag":
		if ok, resp = UnbindTag(tg.UserName, tg.RoomKey); ok {
			return ok, resp
		}
		return false, resp
	case "ListTags":
		if ok, resp = TagList(); ok {
			return ok, resp
		}
		return false, resp
	case "QueryTags":
		if ok, resp = QueryTags(tg.UserName, "ACCOUNT"); ok {
			return ok, resp
		}
		return false, resp
	default:
		resp.Code = "2004"
		resp.Message = "请求参数错误"
		return false, resp
	}
}

// CommonParams 公共参数部分
func CommonParams(pmtp *PushMsgParameter) url.Values {
	pmtp.RegionId = "cn-hangzhou"
	pmtp.Version = "2016-08-01"
	pmtp.AccessKeyId = AliCloudAccessKeyId
	pmtp.SignatureMethod = "HMAC-SHA1"
	pmtp.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
	pmtp.SignatureVersion = "1.0"
	pmtp.SignatureNonce = random.RandomAlphanumeric(16)
	values := make(url.Values)
	values.Add("RegionId", pmtp.RegionId)
	values.Add("AccessKeyId", pmtp.AccessKeyId)
	values.Add("SignatureMethod", pmtp.SignatureMethod)
	values.Add("SignatureVersion", pmtp.SignatureVersion)
	values.Add("SignatureNonce", pmtp.SignatureNonce)
	values.Add("Timestamp", pmtp.Timestamp)
	values.Add("Version", pmtp.Version)
	return values
}

//AddAliMessageTag 添加Tag
func AddAliMessageTag(action string, tagname string, clientKey string) (bool, *AliRespInfo) {
	aliURLPath := "https://cloudpush.aliyuncs.com/?"
	if len(action) < 1 {
		action = "ACCOUNT"
	}
	if strings.ToUpper(action) != "ACCOUNT" || strings.ToUpper(action) != "DEVICE" {
		action = "ACCOUNT"
	}
	if len(clientKey) < 1 {
		return false, &AliRespInfo{}
	}
	if tagname == "" {
		return false, &AliRespInfo{}
	}

	pushMsgTagParaModle := new(PushMsgParameter)
	pushMsgTagParaModle.Action = "BindTag"
	pushMsgTagParaModle.AppKey = AliCloudAppKey
	pushMsgTagParaModle.ClientKey = clientKey
	pushMsgTagParaModle.TagName = tagname
	pushMsgTagParaModle.KeyType = action
	values := CommonParams(pushMsgTagParaModle)

	values.Add("Action", pushMsgTagParaModle.Action)
	values.Add("AppKey", pushMsgTagParaModle.AppKey)
	values.Add("ClientKey", pushMsgTagParaModle.ClientKey)
	values.Add("TagName", pushMsgTagParaModle.TagName)
	values.Add("KeyType", pushMsgTagParaModle.KeyType)
	// 对values  按键名称 排序
	urlStr := url.QueryEscape(values.Encode())
	sourceStr := "POST" + "&" + "%2F" + "&" + urlStr
	sign, _ := Signature(AlicloudAppSecret+"&", sourceStr)
	values.Add("Signature", sign)
	aliURLPath += values.Encode()
	resp, _ := http.Post(aliURLPath, "text/plain", nil)
	ok, respInfo := Ali_resp_to_unmarshal(resp)
	if ok {
		return true, respInfo
	}
	return false, respInfo
}

// TagList 查询全部Tag列表
func TagList() (bool, *AliRespInfo) {
	url_val := "https://cloudpush.aliyuncs.com/?"
	pushMsgTagParaModle := new(PushMsgParameter)
	pushMsgTagParaModle.Action = "ListTags"
	pushMsgTagParaModle.AppKey = AliCloudAppKey
	values := CommonParams(pushMsgTagParaModle)

	values.Add("Action", pushMsgTagParaModle.Action)
	values.Add("AppKey", pushMsgTagParaModle.AppKey)
	values.Add("ClientKey", pushMsgTagParaModle.ClientKey)
	values.Add("TagName", pushMsgTagParaModle.TagName)
	values.Add("KeyType", pushMsgTagParaModle.KeyType)
	// 对values  按键名称 排序
	urlStr := url.QueryEscape(values.Encode())
	sourceStr := "POST" + "&" + "%2F" + "&" + urlStr
	sign, _ := Signature(AlicloudAppSecret+"&", sourceStr)
	values.Add("Signature", sign)
	url_val += values.Encode()
	resp, _ := http.Post(url_val, "text/plain", nil)
	ok, respInfo := Ali_resp_to_unmarshal(resp)
	if ok {
		return true, respInfo
	}
	return false, respInfo
}

// QueryTags 查询某个对象的Tag列表
func QueryTags(clientKey, keyType string) (bool, *AliRespInfo) {
	if len(keyType) < 1 || len(clientKey) < 1 {
		return false, &AliRespInfo{}
	}
	if keyType != "DEVICE" && keyType != "ALIAS" && keyType != "ACCOUNT" {
		return false, &AliRespInfo{}
	}
	url_val := "https://cloudpush.aliyuncs.com/?"
	pushMsgTagParaModle := new(PushMsgParameter)
	pushMsgTagParaModle.Action = "QueryTags"
	pushMsgTagParaModle.AppKey = AliCloudAppKey
	pushMsgTagParaModle.ClientKey = clientKey
	pushMsgTagParaModle.KeyType = keyType

	values := CommonParams(pushMsgTagParaModle)

	values.Add("Action", pushMsgTagParaModle.Action)
	values.Add("AppKey", pushMsgTagParaModle.AppKey)
	values.Add("ClientKey", pushMsgTagParaModle.ClientKey)
	values.Add("KeyType", pushMsgTagParaModle.KeyType)

	// 对values  按键名称 排序
	urlStr := url.QueryEscape(values.Encode())
	sourceStr := "POST" + "&" + "%2F" + "&" + urlStr
	sign, _ := Signature(AlicloudAppSecret+"&", sourceStr)
	values.Add("Signature", sign)
	url_val += values.Encode()

	resp, _ := http.Post(url_val, "text/plain", nil)
	ok, respInfo := Ali_resp_to_unmarshal(resp)
	if ok {
		return true, respInfo
	}
	return false, respInfo
}

// UnbindTag 解绑Tag
func UnbindTag(clientKey string, tags ...string) (bool, *AliRespInfo) {
	if len(clientKey) < 1 {
		return false, &AliRespInfo{}
	}

	if len(tags) > 10 {
		return false, &AliRespInfo{}
	}
	var tagStr string
	if len(tags) > 1 {
		for i, v := range tags {
			if i != 0 {
				tagStr = tagStr + "," + v
			}
			tagStr = v
		}
	} else {
		tagStr = tags[0]
	}
	url_val := "https://cloudpush.aliyuncs.com/?"
	pushMsgTagParaModle := new(PushMsgParameter)
	pushMsgTagParaModle.Action = "UnbindTag"
	pushMsgTagParaModle.AppKey = AliCloudAppKey
	pushMsgTagParaModle.ClientKey = clientKey
	pushMsgTagParaModle.KeyType = "ACCOUNT"
	pushMsgTagParaModle.TagName = tagStr

	values := CommonParams(pushMsgTagParaModle)
	values.Add("Action", pushMsgTagParaModle.Action)
	values.Add("AppKey", pushMsgTagParaModle.AppKey)
	values.Add("ClientKey", pushMsgTagParaModle.ClientKey)
	values.Add("KeyType", pushMsgTagParaModle.KeyType)
	values.Add("TagName", pushMsgTagParaModle.TagName)

	// 对values  按键名称 排序
	urlStr := url.QueryEscape(values.Encode())
	sourceStr := "POST" + "&" + "%2F" + "&" + urlStr
	sign, _ := Signature(AlicloudAppSecret+"&", sourceStr)
	values.Add("Signature", sign)
	url_val += values.Encode()

	resp, _ := http.Post(url_val, "text/plain", nil)
	ok, respInfo := Ali_resp_to_unmarshal(resp)
	if ok {
		return true, respInfo
	}
	return false, respInfo
}
